﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imageViewTool/imageviewtool.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("QtQGraphicsView实现图片放缩、鼠标拖动移动、鼠标点位置放大缩小"));

    QPixmap *backgroundPixmap = new QPixmap(":/new/prefix1/rose.jpg");
    QImage sizedImg = QImage(backgroundPixmap->toImage());
    recvImgShowSignal(sizedImg);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::recvImgShowSignal(QImage image)
{
   // ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   // ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QPixmap ConvertPixmap = QPixmap::fromImage(image);//The QPixmap class is an off-screen image representation that can be used as a paint device
    QGraphicsScene  *qgraphicsScene = new QGraphicsScene;//要用QGraphicsView就必须要有QGraphicsScene搭配着用
    m_Image = new TImgWidget(&ConvertPixmap);//实例化类TImgWidgett的对象m_Image，该类继承自QGraphicsItem，是自己写的类
    int nwith = ui->graphicsView->width();//获取界面控件Graphics View的宽度
    int nheight = ui->graphicsView->height();//获取界面控件Graphics View的高度

    nwith = this->width();
    nheight = this->height();

    m_Image->setQGraphicsViewWH(nwith,nheight);//将界面控件Graphics View的width和height传进类m_Image中
    qgraphicsScene->addItem(m_Image);//将QGraphicsItem类对象放进QGraphicsScene中
    ui->graphicsView->setSceneRect(QRectF(-(nwith/2),-(nheight/2),nwith,nheight));//使视窗的大小固定在原始大小，不会随图片的放大而放大（默认状态下图片放大的时候视窗两边会自动出现滚动条，并且视窗内的视野会变大），防止图片放大后重新缩小的时候视窗太大而不方便观察图片
    ui->graphicsView->setScene(qgraphicsScene);//Sets the current scene to scene. If scene is already being viewed, this function does nothing.
    ui->graphicsView->setFocus();//将界面的焦点设置到当前Graphics View控件
}

